﻿using System;
using System.Collections.Generic;
using System.IO;
using PresentationNamespace;

namespace Infrastructure
{
    public class TxtLogger : ILogger
    {
        private List<string> logMessages;
        
        public TxtLogger()
        {
            logMessages = new List<string>();
        }

        public TxtLogger(List<string> logMessages)
        {
            this.logMessages = new List<string>(logMessages);
        }

        public List<string> GetLogMessages() 
        {
            return new List<string>(logMessages);
        }
        
        public void AddMessage(string message)
        {
            logMessages.Add(message);
        }

        public void WriteMessages(string fileName)
        {
            try
            {
                var writer = new StreamWriter(fileName, false);
                foreach (var message in logMessages)
                {
                    writer.WriteLine(message);
                }
                writer.Close();
            }
            catch (Exception)
            {
                throw new FileNotFoundException();
            }
        }

        public List<string> GetLoggerHistory()
        {
            return logMessages;
        }

        public void ClearLog()
        {
            logMessages.Clear();
        }
    }
}
